package com.caipu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.caipu.dto.UserInfoDTO;
import com.caipu.dto.UserLoginDTO;
import com.caipu.dto.UserRegisterDTO;
import com.caipu.entity.User;
import com.caipu.mapper.UserMapper;
import com.caipu.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    
    @Override
    public UserInfoDTO register(UserRegisterDTO registerDTO) {
        // 检查用户名是否已存在
        User existUser = findByUsername(registerDTO.getUsername());
        if (existUser != null) {
            throw new RuntimeException("用户名已存在");
        }
        
        // 创建新用户
        User user = new User();
        user.setUsername(registerDTO.getUsername());
        user.setPassword(DigestUtils.md5DigestAsHex(registerDTO.getPassword().getBytes()));
        user.setNickname(registerDTO.getNickname());
        user.setCreateTime(LocalDateTime.now());
        user.setUpdateTime(LocalDateTime.now());
        user.setDeleted(0);
        
        save(user);
        
        // 返回用户信息
        UserInfoDTO userInfo = new UserInfoDTO();
        BeanUtils.copyProperties(user, userInfo);
        return userInfo;
    }
    
    @Override
    public UserInfoDTO login(UserLoginDTO loginDTO) {
        User user = findByUsername(loginDTO.getUsername());
        if (user == null) {
            throw new RuntimeException("用户不存在");
        }
        
        String encryptedPassword = DigestUtils.md5DigestAsHex(loginDTO.getPassword().getBytes());
        if (!encryptedPassword.equals(user.getPassword())) {
            throw new RuntimeException("密码错误");
        }
        
        UserInfoDTO userInfo = new UserInfoDTO();
        BeanUtils.copyProperties(user, userInfo);
        return userInfo;
    }
    
    @Override
    public User findByUsername(String username) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        queryWrapper.eq("deleted", 0);
        return getOne(queryWrapper);
    }
    
    @Override
    public boolean updateNickname(Long userId, String nickname) {
        User user = getById(userId);
        if (user == null || user.getDeleted() == 1) {
            return false;
        }
        
        user.setNickname(nickname);
        user.setUpdateTime(LocalDateTime.now());
        return updateById(user);
    }
    
    @Override
    public boolean updatePassword(Long userId, String oldPassword, String newPassword) {
        User user = getById(userId);
        if (user == null || user.getDeleted() == 1) {
            return false;
        }
        
        // 验证原密码
        String encryptedOldPassword = DigestUtils.md5DigestAsHex(oldPassword.getBytes());
        if (!encryptedOldPassword.equals(user.getPassword())) {
            return false;
        }
        
        // 更新新密码
        String encryptedNewPassword = DigestUtils.md5DigestAsHex(newPassword.getBytes());
        user.setPassword(encryptedNewPassword);
        user.setUpdateTime(LocalDateTime.now());
        return updateById(user);
    }
}